package com.example.bobo.yuejian322.Activity;

import android.content.Intent;
import android.media.Image;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.bobo.yuejian322.Interfa.RegSeverice;
import com.example.bobo.yuejian322.R;
import com.example.bobo.yuejian322.bean.Login;
import com.example.bobo.yuejian322.bean.Reg;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class RegisterActivity extends AppCompatActivity {

    private EditText tel,pass;
    private Button reg;
    private ImageButton back;
    private ImageView eye;
    private String  user_name;
    private int  number=1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        tel= (EditText) findViewById(R.id.name);
        pass= (EditText) findViewById(R.id.pass);
        reg= (Button) findViewById(R.id.reg);
        back= (ImageButton) findViewById(R.id.back);
        eye= (ImageView) findViewById(R.id.eyes);
           setOnclick();
    }
  //监听事件处理
    public  void setOnclick(){
      //返回
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
       //注册事件处理
        reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Retrofit retrofit = new Retrofit.Builder()
                        .baseUrl("http://172.24.10.175/workout/api.php/")
                        .addConverterFactory(GsonConverterFactory.create())
                        .build();
                    String strname=tel. getText().toString();
                    String strpass=pass. getText().toString();

                    RegSeverice regSeverice =retrofit.create(RegSeverice.class);

                    Call<Reg> regCall =regSeverice.userReg(strname,strpass);

                    regCall.enqueue(new Callback<Reg>() {
                     @Override
                     public void onResponse(Call<Reg> call, Response<Reg> response) {
                         Reg reg = response.body();
                          if(reg.getError()==null){
                              Toast.makeText(getApplicationContext(), "注册成功", Toast.LENGTH_SHORT).show();

                              String strName=tel. getText().toString();
                             // Toast.makeText(getApplicationContext(), strName, Toast.LENGTH_SHORT).show();
                              Intent data = new Intent();
                              data.putExtra(user_name,strName);
                              setResult(RESULT_OK,data);

                              finish();
                          }
                         else {
                              Toast.makeText(getApplicationContext(), "该手机号码已存在", Toast.LENGTH_SHORT).show();
                          }
                     }
                     @Override
                     public void onFailure(Call<Reg> call, Throwable t) {
                     }
                 });

            }
        });
     //显示密码
       eye.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {

               switch (number){
                   case  1:
                       //隐藏密码
                       pass.setTransformationMethod(PasswordTransformationMethod.getInstance());
                       number = 0;
                    break;
                   case  0:

                       //显示密码
                       pass.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                       number = 1;
                       break;
               }








           }
       });


    }








}


